﻿using Custom.Framework;
using Custom.Infrastructure.EventBus;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Controls
{
    public partial class ProcessForm : Form
    {
        /// <summary>
        /// 进度条最大值
        /// </summary>
        public int Max { get; set; }

        public ProcessForm()
        {
            InitializeComponent();

            if (this.Owner != null)
            {
                this.StartPosition = FormStartPosition.CenterParent;
            }
            else
            {
                this.StartPosition = FormStartPosition.CenterScreen;
            }
        }

        protected virtual void CallBack(TEventEntity entity, bool result, Exception ex)
        {

        }

        public void SetValueText(int value, string text)
        {
            try
            {
                this.Invoke(new Action(() =>
                {
                    if (this.progressBar.Maximum != Max)
                    {
                        this.progressBar.Maximum = Max;
                    }

                    this.progressBar.Value = value > Max ? Max : value;

                    Application.DoEvents();

                    if (!string.IsNullOrEmpty(text))
                    {
                        if (text.Length > 35)
                        {
                            this.lbDescription.Text = text.Substring(0, 35) + "...";
                        }
                        else
                        {
                            this.lbDescription.Text = text;
                        }
                    }

                    this.lbPercent.Text = $"{value}/{this.Max}";


                    Application.DoEvents();
                }));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private void ProcessForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void ProcessForm_FormClosed(object sender, FormClosedEventArgs e)
        {
        }
    }
}
